//=====================================================================
//    COPYRIGHT(C) Innobeam
//    ALL RIGHTS RESERVED
//=====================================================================
//Filename    : LCD_INIT.v
//Created On  : 2019-12-09
//Author      : haoyu.xiao
//Description : LCD initialization
//Include     : 
//Modification: 
//=====================================================================
`timescale	1ns/100ps
`include	"../rtl/def_command.v"
module LCD_INIT(
	iClk,
	iRst_n,
	
	ovLCD_SPI_TxData,
	oLCD_SPI_Start,
	oLCD_DC,
	ivLCD_SPI_RxData,
	iLCD_SPI_Done,
		
	iLCD_Start_Init,
	oLCD_Init_Done
	);
	
//========================================================================
//    parameter
//========================================================================
//command
	parameter		LCD_IDLE = 8'h00;
	parameter		LCD_SWRESET = `LCD_SWRESET;
	parameter		LCD_RGB_INTERFACE = `LCD_RGB_INTERFACE;
	parameter		LCD_INTERFACE = `LCD_INTERFACE;
	parameter		LCD_POWERA = `LCD_POWERA;
	parameter		LCD_POWERB = `LCD_POWERB;
	parameter 		LCD_POWER1 = `LCD_POWER1;  /* Power Control 1 register */
	parameter 		LCD_POWER2 = `LCD_POWER2;   /* Power Control 2 register */
	parameter 		LCD_VCOM1  = `LCD_VCOM1;   /* VCOM Control 1 register */
	parameter 		LCD_VCOM2  = `LCD_VCOM2;   /* VCOM Control 2 register */
	parameter 		LCD_DTCA = `LCD_DTCA;  /* Driver timing control A */
	parameter 		LCD_DTCB = `LCD_DTCB;  /* Driver timing control B */
	parameter 		LCD_POWER_SEQ = `LCD_POWER_SEQ; /* Power on sequence register */
	parameter 		LCD_3GAMMA_EN = `LCD_3GAMMA_EN; /* 3 Gamma enable register */
	parameter 		LCD_PRC = `LCD_PRC; /* Pump ratio control register */
	parameter		LCD_MAC = `LCD_MAC; /* Memory Access Control register*/
	parameter		LCD_PIXEL_FORMAT = `LCD_PIXEL_FORMAT; /* Pixel Format register */
	parameter       LCD_FRMCTR1 = `LCD_FRMCTR1;/* Frame Rate Control (In Normal Mode) */
	parameter		LCD_GAMMA = `LCD_GAMMA;/* Gamma register */
	parameter		LCD_PGAMMA = `LCD_PGAMMA;   /* Positive Gamma Correction register */
	parameter		LCD_NGAMMA = `LCD_NGAMMA;   /* Negative Gamma Correction register */
	parameter		LCD_DFC = `LCD_DFC;/* Display Function Control register */
	parameter		LCD_SLEEP_OUT = `LCD_SLEEP_OUT;
	parameter		LCD_DISPLAY_ON = `LCD_DISPLAY_ON;
	parameter		LCD_DISPLAY_OFF = `LCD_DISPLAY_OFF;
	parameter		LCD_PAGE_ADDR = `LCD_PAGE_ADDR;
	parameter		LCD_COLUMN_ADDR = `LCD_COLUMN_ADDR;
	parameter		LCD_GRAM = `LCD_GRAM;

//data	 
	parameter		LCD_POWERA_PARAMETER1 = `LCD_POWERA_PARAMETER1;
	parameter		LCD_POWERA_PARAMETER2 = `LCD_POWERA_PARAMETER2;
	parameter		LCD_POWERA_PARAMETER3 = `LCD_POWERA_PARAMETER3;	
	parameter		LCD_POWERA_PARAMETER4 = `LCD_POWERA_PARAMETER4;	
	parameter		LCD_POWERA_PARAMETER5 = `LCD_POWERA_PARAMETER5;
	
	parameter		LCD_POWERB_PARAMETER1 = `LCD_POWERB_PARAMETER1;
	parameter		LCD_POWERB_PARAMETER2 = `LCD_POWERB_PARAMETER2;
	parameter		LCD_POWERB_PARAMETER3 = `LCD_POWERB_PARAMETER3;
	
	parameter	 	LCD_DTCA_PARAMETER1 = `LCD_DTCA_PARAMETER1;		
	parameter		LCD_DTCA_PARAMETER2 = `LCD_DTCA_PARAMETER2;		
	parameter		LCD_DTCA_PARAMETER3 = `LCD_DTCA_PARAMETER3;		
	parameter 		LCD_DTCB_PARAMETER1	= `LCD_DTCB_PARAMETER1;	
	parameter 		LCD_DTCB_PARAMETER2	= `LCD_DTCB_PARAMETER2;	
	
	parameter 		LCD_POWER_SEQ_PARAMETER1 = `LCD_POWER_SEQ_PARAMETER1;		
	parameter 		LCD_POWER_SEQ_PARAMETER2 = `LCD_POWER_SEQ_PARAMETER2;		
	parameter 		LCD_POWER_SEQ_PARAMETER3 = `LCD_POWER_SEQ_PARAMETER3;		
	parameter 		LCD_POWER_SEQ_PARAMETER4 = `LCD_POWER_SEQ_PARAMETER4;		
			
	parameter 		LCD_PRC_PARAMETER1 = `LCD_PRC_PARAMETER1;
			
	parameter 		LCD_POWER1_PARAMETER1 = `LCD_POWER1_PARAMETER1;
		
	parameter 		LCD_POWER2_PARAMETER1 = `LCD_POWER2_PARAMETER1;
	
	parameter 		LCD_VCOM1_PARAMETER1 = `LCD_VCOM1_PARAMETER1;
	parameter 		LCD_VCOM1_PARAMETER2 = `LCD_VCOM1_PARAMETER2;
			
	parameter 		LCD_VCOM2_PARAMETER1 = `LCD_VCOM2_PARAMETER1;
			
	parameter 		LCD_MAC_PARAMETER1 = `LCD_MAC_PARAMETER1;
			
	parameter 		LCD_PIXEL_FORMAT_PARAMETER1	= `LCD_PIXEL_FORMAT_PARAMETER1;
			
	parameter 		LCD_FRMCTR1_PARAMETER1 = `LCD_FRMCTR1_PARAMETER1;
	parameter 		LCD_FRMCTR1_PARAMETER2 = `LCD_FRMCTR1_PARAMETER2;
			
	parameter 		LCD_DFC_PARAMETER1 = `LCD_DFC_PARAMETER1;
	parameter 		LCD_DFC_PARAMETER2 = `LCD_DFC_PARAMETER2;
	parameter 		LCD_DFC_PARAMETER3 = `LCD_DFC_PARAMETER3;
	parameter 		LCD_DFC_PARAMETER4 = `LCD_DFC_PARAMETER4;
	
	parameter 		LCD_3GAMMA_EN_PARAMETER1 = `LCD_3GAMMA_EN_PARAMETER1;
			
	parameter 		LCD_GAMMA_PARAMETER1 = `LCD_GAMMA_PARAMETER1;
			
	parameter 		LCD_PGAMMA_PARAMETER1 = `LCD_PGAMMA_PARAMETER1;
	parameter 		LCD_PGAMMA_PARAMETER2 = `LCD_PGAMMA_PARAMETER2;
	parameter 		LCD_PGAMMA_PARAMETER3 = `LCD_PGAMMA_PARAMETER3;
	parameter 		LCD_PGAMMA_PARAMETER4 = `LCD_PGAMMA_PARAMETER4;
	parameter 		LCD_PGAMMA_PARAMETER5 = `LCD_PGAMMA_PARAMETER5;
	parameter 		LCD_PGAMMA_PARAMETER6 = `LCD_PGAMMA_PARAMETER6;
	parameter 		LCD_PGAMMA_PARAMETER7 = `LCD_PGAMMA_PARAMETER7;
	parameter 		LCD_PGAMMA_PARAMETER8 = `LCD_PGAMMA_PARAMETER8;
	parameter 		LCD_PGAMMA_PARAMETER9 = `LCD_PGAMMA_PARAMETER9;
	parameter 		LCD_PGAMMA_PARAMETER10 = `LCD_PGAMMA_PARAMETER10;
	parameter 		LCD_PGAMMA_PARAMETER11 = `LCD_PGAMMA_PARAMETER11;	//
	parameter 		LCD_PGAMMA_PARAMETER12 = `LCD_PGAMMA_PARAMETER12;
	parameter 		LCD_PGAMMA_PARAMETER13 = `LCD_PGAMMA_PARAMETER13;	//
	parameter 		LCD_PGAMMA_PARAMETER14 = `LCD_PGAMMA_PARAMETER14;	//
	parameter 		LCD_PGAMMA_PARAMETER15 = `LCD_PGAMMA_PARAMETER15;
			
	parameter 		LCD_NGAMMA_PARAMETER1 = `LCD_NGAMMA_PARAMETER1;
	parameter 		LCD_NGAMMA_PARAMETER2 = `LCD_NGAMMA_PARAMETER2;
	parameter 		LCD_NGAMMA_PARAMETER3 = `LCD_NGAMMA_PARAMETER3;
	parameter 		LCD_NGAMMA_PARAMETER4 = `LCD_NGAMMA_PARAMETER4;
	parameter 		LCD_NGAMMA_PARAMETER5 = `LCD_NGAMMA_PARAMETER5;
	parameter 		LCD_NGAMMA_PARAMETER6 = `LCD_NGAMMA_PARAMETER6;
	parameter 		LCD_NGAMMA_PARAMETER7 = `LCD_NGAMMA_PARAMETER7;
	parameter 		LCD_NGAMMA_PARAMETER8 = `LCD_NGAMMA_PARAMETER8;
	parameter 		LCD_NGAMMA_PARAMETER9 = `LCD_NGAMMA_PARAMETER9;
	parameter 		LCD_NGAMMA_PARAMETER10 = `LCD_NGAMMA_PARAMETER10;
	parameter 		LCD_NGAMMA_PARAMETER11 = `LCD_NGAMMA_PARAMETER11;	//
	parameter 		LCD_NGAMMA_PARAMETER12 = `LCD_NGAMMA_PARAMETER12;
	parameter 		LCD_NGAMMA_PARAMETER13 = `LCD_NGAMMA_PARAMETER13;	//
	parameter 		LCD_NGAMMA_PARAMETER14 = `LCD_NGAMMA_PARAMETER14;	//
	parameter 		LCD_NGAMMA_PARAMETER15 = `LCD_NGAMMA_PARAMETER15;
	
	parameter		LCD_PAGE_ADDR_START = `LCD_PAGE_ADDR_START;
	parameter		LCD_PAGE_ADDR_END = `LCD_PAGE_ADDR_END;
	parameter		LCD_COLUMN_ADDR_START = `LCD_COLUMN_ADDR_START;
	parameter		LCD_COLUMN_ADDR_END = `LCD_COLUMN_ADDR_END;
	parameter		BGcolor = `BGcolor;
//========================================================================
//    port
//========================================================================	
	input					iClk;
	input					iRst_n;
	
	output		[7:0]		ovLCD_SPI_TxData;
	output					oLCD_SPI_Start;
	output					oLCD_DC;
	input		[7:0]		ivLCD_SPI_RxData;
	input					iLCD_SPI_Done;
		
	input					iLCD_Start_Init;
	output					oLCD_Init_Done;
//========================================================================
//    signal
//========================================================================
	reg					rLCD_SPI_Start;
	reg					rLCD_DC;
	reg		[7:0]		rvLCD_SPI_TxData;
	reg					rLCD_Init_Done;
	reg					rWait20msStart;
	reg		[7:0]		rvState;
	wire				wWait20msDone;
	reg		[31:0]		rvByteCnt;
//========================================================================
	assign 				oLCD_SPI_Start = rLCD_SPI_Start;
	assign 				oLCD_Init_Done = rLCD_Init_Done;
	assign 				oLCD_DC = rLCD_DC;
	assign				ovLCD_SPI_TxData = rvLCD_SPI_TxData;
//========================================================================
	Wait20ms Wait20ms_inst(
	.iClk				(iClk),
	.iRst_n				(iRst_n),
	
	.iWait20msStart		(rWait20msStart),
	.oWait20msDone		(wWait20msDone)
	);
//========================================================================
	//FSM
	always@(posedge iClk or negedge iRst_n)begin
			if(!iRst_n)begin	
				rLCD_SPI_Start		<= 1'b0;
				rLCD_DC				<= 1'b0;
				rvLCD_SPI_TxData 	<= 8'h0;
				rLCD_Init_Done		<= 1'b0;
				rWait20msStart		<= 1'b0;
				rvByteCnt			<= 32'd0;
				rvState				<= 8'h0;
				end
			else begin
				case (rvState)
				//waiting for initialization
					8'd0: begin			
						rLCD_SPI_Start		<= 1'b0;
						rLCD_DC				<= 1'b0;
						rvLCD_SPI_TxData 	<= 8'h0;					
						rLCD_Init_Done <= 1'b0;					
						if (iLCD_Start_Init) begin		
							rvState <= 8'h1;
							end
						end
					//LCD software reset and delay 20ms
					8'd1: begin	
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_SWRESET;
						rLCD_Init_Done <= 1'b0;
						rvState	<= rvState + 1'd1;						
						end	
					8'd2: begin
						rLCD_SPI_Start <= 1'b0;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= rvLCD_SPI_TxData;
						rLCD_Init_Done <= 1'b0;
						if(iLCD_SPI_Done == 1'b1)begin
							rvState	<= rvState + 1'd1;
						end	
						end
					8'd3: begin	
						rWait20msStart <= 1'b1;
						rvState <= rvState + 8'd1;
						end					
					8'd4: begin
						rWait20msStart <= 1'b0;
						if(wWait20msDone) begin
							rvState <= rvState + 8'd1;
							end	
						end
					//wait 1 more clk to let spi master pull Done signal down.
					8'd6,8'd8,8'd10,8'd12,8'd14,8'd16,8'd18,8'd20,8'd22,8'd24,
					8'd26,8'd28,8'd30,8'd32,8'd34,8'd36,8'd38,8'd40,8'd42,8'd44,
					8'd46,8'd48,8'd50,8'd52,8'd54,8'd56,8'd58,8'd60,8'd62,8'd64,
					8'd66,8'd68,8'd70,8'd72,8'd74,8'd76,8'd78,8'd80,8'd82,8'd84,
					8'd86,8'd88,8'd90,8'd92,8'd94,8'd96,8'd98,8'd100,8'd102,8'd104,
					8'd106,8'd108,8'd110,8'd112,8'd114,8'd116,8'd118,8'd120,8'd122,8'd124,
					8'd126,8'd128,8'd130,8'd132,8'd134,8'd136,8'd138,8'd140,8'd142,8'd144,
					8'd146,8'd148,8'd150,8'd152,8'd154,8'd156,8'd158,8'd160,8'd162,8'd164,
					8'd166,8'd168,8'd170,8'd172,8'd174,8'd176,8'd178,8'd180,8'd182,8'd184,
					8'd186,8'd189: begin
						rLCD_SPI_Start <= 1'b0;
						rLCD_DC	<= rLCD_DC;
						rvLCD_SPI_TxData <= rvLCD_SPI_TxData;
						rLCD_Init_Done <= 1'b0;
						if(iLCD_SPI_Done == 1'b1)begin
							rvState	<= rvState + 1'd1;
						end	
						end
					 /* Power control A register */
					8'd5: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_POWERA;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd7: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERA_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd9: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERA_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd11: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERA_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd13: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERA_PARAMETER4;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd15: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERA_PARAMETER5;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Power control B register */
					8'd17: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_POWERB;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd19: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERB_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd21: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERB_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd23: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWERB_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					/* Driver timing control A */
					8'd25: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_DTCA;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd27: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DTCA_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd29: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DTCA_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd31: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DTCA_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					/* Driver timing control B */
					8'd33: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_DTCB;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd35: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DTCB_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd37: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DTCB_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					/* Power on sequence register */
					8'd39: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_POWER_SEQ;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd41: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER_SEQ_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd43: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER_SEQ_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd45: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER_SEQ_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;	
						end
					8'd47: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER_SEQ_PARAMETER4;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;
						end				
					/* Pump ratio control register */
					8'd49: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_PRC;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd51: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PRC_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					/* Power Control 1 register */
					8'd53: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_POWER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd55: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER1_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end					
					/* Power Control 2 register */
					8'd57: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_POWER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd59: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_POWER2_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					 /* VCOM Control 1 register */
					8'd61: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_VCOM1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd63: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_VCOM1_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd65: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_VCOM1_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end							
					 /* VCOM Control 2 register */
					8'd67: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_VCOM2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd69: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_VCOM2_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Memory Access Control register*/
					8'd71: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_MAC;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd73: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_MAC_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Pixel Format register */
					8'd75: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_PIXEL_FORMAT;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd77: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PIXEL_FORMAT_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					 /* Frame Rate Control (In Normal Mode) */
					8'd79: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_FRMCTR1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd81: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_FRMCTR1_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					8'd83: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_FRMCTR1_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					/* Display Function Control register */	
					8'd85: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_DFC;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd87: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DFC_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					8'd89: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DFC_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd91: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_DFC_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					/* 3 Gamma enable register */	
					8'd93: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_3GAMMA_EN;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd95: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_3GAMMA_EN_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Gamma register */
					8'd97: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_GAMMA;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd99: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_GAMMA_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Positive Gamma Correction register */
					8'd101: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_PGAMMA;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd103: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd105: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd107: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd109: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER4;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd111: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER5;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd113: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER6;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd115: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER7;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd117: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER8;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd119: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER9;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd121: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER10;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd123: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER11;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd125: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER12;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd127: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER13;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd129: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER14;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd131: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_PGAMMA_PARAMETER15;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					/* Negative Gamma Correction register */	
					8'd133: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_NGAMMA;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd135: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER1;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd137: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER2;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd139: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER3;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd141: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER4;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd143: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER5;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd145: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER6;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd147: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER7;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd149: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER8;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd151: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER9;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd153: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER10;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd155: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER11;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd157: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER12;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd159: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER13;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd161: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER14;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd163: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= LCD_NGAMMA_PARAMETER15;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end		
					/* Page address register */	
					8'd165: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_PAGE_ADDR;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd167: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h00;//LCD_PAGE_ADDR_START[15:8];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd169: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h00;//LCD_PAGE_ADDR_START[7:0];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd171: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h01;//LCD_PAGE_ADDR_END[15:8];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd173: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h3F;//LCD_PAGE_ADDR_END[7:0];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end		
					/* Colomn address register */
					8'd175: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_COLUMN_ADDR;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd177: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h00;//LCD_COLUMN_ADDR_START[15:8];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end	
					8'd179: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;										
						rvLCD_SPI_TxData <= 8'h00;//LCD_COLUMN_ADDR_START[7:0];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
					8'd181: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'h00;//LCD_COLUMN_ADDR_END[15:8];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;						
						end
					8'd183: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rvLCD_SPI_TxData <= 8'hEF;//LCD_COLUMN_ADDR_END[7:0];
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end	
						
					8'd185: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_GRAM;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end		
						
					8'd187: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rLCD_Init_Done <= 1'b0;	

						if(iLCD_SPI_Done == 1'b1 && rvByteCnt == 32'd153599)begin//rvByteCnt == 32'd1823
							rvState	<= rvState + 1'd1;
							rvByteCnt <= 32'd0;
							end
						else if(iLCD_SPI_Done == 1'b1)begin
							rvState <= rvState;
							rvByteCnt <= rvByteCnt +32'd1;
							end
						else begin
							rvState <= rvState;
							rvByteCnt <= rvByteCnt;
						end	
											
					if(rvByteCnt[0] == 1'b0)				
						rvLCD_SPI_TxData <= BGcolor[15:8];//8'h21;
					else 
						rvLCD_SPI_TxData <= BGcolor[7:0];//8'h87;
					end	
					
					8'd188: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= LCD_GRAM;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= rvState + 1'd1;				
						end		
					8'd190: begin
						rLCD_SPI_Start <= 1'b1;
						rLCD_DC	<= 1'b1;
						rLCD_Init_Done <= 1'b0;	

						if(iLCD_SPI_Done == 1'b1 && rvByteCnt == 32'd153599)begin//rvByteCnt == 32'd1823
							rvState	<= rvState + 1'd1;
							rvByteCnt <= 32'd0;
							end
						else if(iLCD_SPI_Done == 1'b1)begin
							rvState <= rvState;
							rvByteCnt <= rvByteCnt +32'd1;
							end
						else begin
							rvState <= rvState;
							rvByteCnt <= rvByteCnt;
						end	
											
					if(rvByteCnt[0] == 1'b0)				
						rvLCD_SPI_TxData <= BGcolor[15:8];//8'h21;
					else 
						rvLCD_SPI_TxData <= BGcolor[7:0];//8'h87;
					end
					
					8'd191: begin
						rLCD_SPI_Start <= 1'b0;
						rLCD_DC	<= rLCD_DC;
						rvLCD_SPI_TxData <= rvLCD_SPI_TxData;				
						rvState	<= 8'h0;
						rLCD_Init_Done <= 1'b1;				
						end	
					default:begin
						rLCD_SPI_Start <= 1'b0;
						rLCD_DC	<= 1'b0;
						rvLCD_SPI_TxData <= 8'h0;
						rLCD_Init_Done <= 1'b0;	
						rvState	<= 8'h0;		
						rWait20msStart <= 1'b0;		
						end
					endcase
				end
			end

endmodule